package com.lingx.model.impl.executor;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lingx.model.IContext;
import com.lingx.model.IExecutor;
import com.lingx.model.IExtExecutor;
import com.lingx.model.IPerformer;
import com.lingx.service.InterpretService;
import com.lingx.service.ModelService;
import com.lingx.service.QueryService;
import com.lingx.service.ReportService;
import com.lingx.utils.Utils;
import com.lingx.web.api.IApi;
@Component
public class GridExecutor implements IExecutor,Serializable{
	private final static Logger logger = LoggerFactory.getLogger(GridExecutor.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 3864106758437224344L;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private QueryService queryService;
	@Resource
	private ModelService modelService;
	@Resource
	private InterpretService interpretService;
	@Resource
	private ReportService reportService;
	@Resource
	private List<IExtExecutor> listExtExecutor;
	
	@Override
	public String getId() {
		return "GridExecutor";
	}

	@Override
	public String getCode() {
		return "GridExecutor";
	}

	@Override
	public String getName() {
		return "GridExecutor";
	}

	@Override
	public String getIconCls() {
		return "GridExecutor";
	}

	@Override
	public Boolean getLeaf() {
		return true;
	}

	@Override
	public String getModelType() {
		return "GridExecutor";
	}

	@Override
	public void setId(String id) {
		
	}

	@Override
	public void setAuthor(String author) {
		
	}

	@Override
	public void setLastModifyTime(String lastModifyTime) {
		
	}

	@Override
	public Object execute(Map<String, Object> params,IContext context, IPerformer performer) throws Exception {
		Map<String,Object> ret=IApi.getRetMap();
		String extparam=context.getEntity().getExtparam();
		//当配置中有标明GRID要特殊处理时
		if(Utils.isNotNull(extparam)&&extparam.contains("GridExecutor:")) {
			String code=extparam.substring(extparam.indexOf("GridExecutor:"));
			code=code.replace("GridExecutor:", "");
			if(code.indexOf(";")>0) {
				code=code.substring(0, code.indexOf(";"));
			}
			System.out.println("GridExecutor->code:"+code);
			for(IExtExecutor eexe:listExtExecutor) {
				if(code.equals(eexe.getCode())) {
					Map<String,Object> retMap=(Map<String,Object>)eexe.execute(params, context, performer);
					List<Map<String,Object>> rows=(List<Map<String,Object>>)retMap.get("rows");
					rows=this.interpretService.outputFormat(rows, context.getEntity().getFields().getList(), context.getEntity());
					retMap.put("rows", rows);
					return retMap;
				}
			}
		}
		String sql=this.queryService.getGridSelectSql(params, context, performer);
		sql=Utils.sqlInjection(sql);
		//System.out.println("=================================GridExecutor===================================");
		//System.out.println(sql);
		logger.info(sql);
		try {
			this.reportService.putSqlToSession( sql,context.getEntity().getCode(), context.getUserBean().getId());
			List<Map<String,Object>> rows=this.jdbcTemplate.queryForList(sql);
			rows=this.interpretService.outputFormat(rows, context.getEntity().getFields().getList(), context.getEntity());
			ret.put("rows", rows);
			ret.put("total", this.jdbcTemplate.queryForObject(this.queryService.getCountSql(sql), Integer.class));
			return ret;
		} catch (DataAccessException e) {
			System.out.println("=================================GridExecutor===================================");
			System.out.println(sql);
			System.out.println(JSON.toJSONString(params));
			e.printStackTrace();
			return null;
		}
	}

}
